/*
 * BanBu_MagicFaceViewController
 * @description 本文件提供对话中魔法工具箱中的魔法表情的viewcontroller
 * @package
 * @author          ***
 * @copyright
 * @version 		1.0
 * @description 本文件提供对话中魔法工具箱中的魔法表情的viewcontroller
 */

#import "BanBu_MagicFaceViewController.h"
#import "BanBu_ImageView.h"
#import "BanBu_NavButton.h"
#import "BanBu_MagicFaceCell.h"
#import "BanBu_MagicDetailViewController.h"
#import "AppCommunicationManager.h"
#import "TKLoadingView.h"
#import "AppDataManager.h"

@interface BanBu_MagicFaceViewController ()

@end

@implementation BanBu_MagicFaceViewController
-(void)viewWillAppear:(BOOL)animated
{
    [UIView setAnimationsEnabled:YES];
    [super viewWillAppear:animated];
}
- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = NSLocalizedString(@"magicGifTitle", nil);
    
    self.tableView.backgroundColor = [UIColor colorWithRed:255.0/255 green:250.0/255 blue:240.0/255 alpha:1.0];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.allowsSelectionDuringEditing = YES;
    
    BanBu_ImageView *img = [[[BanBu_ImageView alloc] initWithBackGroundImageType:UIImageViewCreatTypeTable] autorelease];
    self.tableView.backgroundView = img;
    
    //返回按钮
//    [self createBackButton];
}

-(void)dealloc
{
    [_magicFaceKindArray release],_magicFaceKindArray = nil;
    [super dealloc];
}

//- (void)createBackButton
//{
//    BanBu_NavButton *btn_return=[[BanBu_NavButton alloc] initWithButtonType:UIButtonCreatTypeBack];
//    btn_return.position = ButtonPositionLeft;
//    [btn_return addTarget:self action:@selector(popself) forControlEvents:UIControlEventTouchUpInside];
//    btn_return.titleLabel.font = [UIFont systemFontOfSize:14];
//    
//    UIBarButtonItem *bar_itemreturn = nil;
//    
//    CGFloat btnLen = [NSLocalizedString(@"returnButton", nil) sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(100, 30)].width;
//    
//    btn_return.frame=CGRectMake(0, 0, btnLen+20, 30);
//    
//    [btn_return setTitle:NSLocalizedString(@"returnButton", nil) forState:UIControlStateNormal];
//    
//    bar_itemreturn=[[[UIBarButtonItem alloc] initWithCustomView:btn_return] autorelease];
//    
//    self.navigationItem.leftBarButtonItem = bar_itemreturn;
//}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)popself
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - Table view data source

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSString * title = nil;

    NSDictionary * dic = [self.magicFaceKindArray objectAtIndex:section];
    
    if ([[MyAppDataManager getLanguageSubmitToServer] isEqualToString:@"cn"])
    {
        title = [dic valueForKey:@"title_cn"];
    }
    else if([[MyAppDataManager getLanguageSubmitToServer] isEqualToString:@"jp"])
    {
        title = [dic valueForKey:@"title_jp"];
    }
    else
    {
        title = [dic valueForKey:@"title_en"];
    }
    
    return title;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.magicFaceKindArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    BanBu_MagicFaceCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil)
    {
        cell = [[BanBu_MagicFaceCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    NSDictionary * Dic = [self.magicFaceKindArray objectAtIndex:indexPath.section];
    NSArray * imageUrlArray = [Dic valueForKey:@"listof"];
    [cell clearImage];
    [cell setImageUrl:imageUrlArray];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    [TKLoadingView showTkloadingAddedTo:self.navigationController.view title:NSLocalizedString(@"chuliNotice", nil) activityAnimated:YES];
    self.navigationController.view.userInteractionEnabled = NO;
    
    NSString * magicid = [[self.magicFaceKindArray objectAtIndex:indexPath.section] valueForKey:@"id"];
    NSString * title = [self tableView:tableView titleForHeaderInSection:indexPath.section];
    NSDictionary * parDic = [NSDictionary dictionaryWithObjectsAndKeys:magicid,@"magicid",title,@"title",nil];

    [AppComManager getBanBuData:BanBu_GetMagicGid_detail par:parDic delegate:self];
}

- (void)banbuRequestDidFinished:(NSDictionary *)resDic error:(NSError *)error
{
    self.navigationController.view.userInteractionEnabled = YES;
    [TKLoadingView dismissTkFromView:self.navigationController.view animated:YES afterShow:0.0];
    if(error)
    {
        if([error.domain isEqualToString:BanBuDataformatError])
        {
            [TKLoadingView showTkloadingAddedTo:self.navigationController.view title:NSLocalizedString(@"network_error", nil) activityAnimated:NO duration:2.0];
        }
        else
        {
            [TKLoadingView showTkloadingAddedTo:self.navigationController.view title:NSLocalizedString(@"network_error", nil) activityAnimated:NO duration:2.0];
        }
        return;
    }
    
    if ([AppComManager respondsDic:resDic isFunctionData:BanBu_GetMagicGid_detail])
    {
        if([[resDic valueForKey:@"ok"] boolValue])
        {
            BanBu_MagicDetailViewController * magicDetail = [[BanBu_MagicDetailViewController alloc]init];
            magicDetail.magicUrlList = [resDic valueForKey:@"list"];
            magicDetail.title = [MyAppDataManager IsInternationalLanguage:[resDic valueForKey:@"title"]];
            magicDetail.chat = self.chat;
            [self.navigationController pushViewController:magicDetail animated:YES];
            [magicDetail release];
        }
        else
        {
            [TKLoadingView showTkloadingAddedTo:self.navigationController.view title:[MyAppDataManager IsInternationalLanguage:[resDic valueForKey:@"error"]] activityAnimated:NO duration:1.0];
        }
    }
}

@end
